package org.zjx.route.model;

import com.alibaba.fastjson2.annotation.JSONType;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.zjx.route.entity.Location;
import org.zjx.route.entity.RouteType;

import java.util.Map;

/**
 * 飞行符传送路由类
 * 表示使用飞行符进行传送的路径
 */

@JSONType(typeName = "FlyingCharmRoute")
@EqualsAndHashCode(callSuper = true)
@Data
@NoArgsConstructor
public class FlyingCharmRoute extends AbstractRoute {

    // 预设的飞行符目的地
    public static final Map<String, Location> DESTINATIONS = Map.of(
            "长安城", new Location("长安城", 361, 243),
            "傲来国", new Location("傲来国", 123, 94),
            "长寿村", new Location("长寿村", 111, 61),
            "朱紫国", new Location("朱紫国", 140, 93),
            "西梁女国", new Location("西梁女国", 101, 28),
            "宝象国", new Location("宝象国", 115, 70),
            "建邺城", new Location("建邺城", 65, 30)
    );

    private String destinationCity;  // 目标城市名
    private int instanceId;  // 飞行旗ID

    /**
     * 构造函数
     *
     * @param start    起点
     * @param cityName 城市名
     * @throws IllegalArgumentException 如果起点和终点在同一城市
     */
    public FlyingCharmRoute(Location start, String cityName, int instanceId) {
        super(start, getDestination(cityName), 10, RouteType.FLYING_CHARM, false);
        if (!DESTINATIONS.containsKey(cityName)) {
            throw new IllegalArgumentException("不支持的目的地: " + cityName);
        }
        this.destinationCity = cityName;
        this.instanceId = instanceId;
    }

    /**
     * 根据城市名获取目的地
     *
     * @param cityName 城市名
     * @return 目的地位置
     * @throws IllegalArgumentException 如果城市名不支持
     */
    private static Location getDestination(String cityName) {
        Location dest = DESTINATIONS.get(cityName);
        if (dest == null) {
            throw new IllegalArgumentException("Unsupported flying charm destination: " + cityName);
        }
        return dest;
    }

    /**
     * 创建反向路由(不支持)
     *
     * @throws UnsupportedOperationException 总是抛出
     */
    @Override
    protected Route createReverseRoute() {
        throw new UnsupportedOperationException("飞行符只能单向使用");
    }

    /**
     * 获取路由描述
     *
     * @return 描述字符串
     */
    @Override
    public String getDescription() {
        return String.format("使用飞行符#%d从%s飞往%s，耗时%d秒",
                instanceId, getStart().getSceneName(), destinationCity, getTime());
    }
}